package news;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import user.UserDao;

/**
 * Servlet implementation class DeleteNewsServlet
 */
@WebServlet("/DeleteNewsServlet")
public class DeleteNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteNewsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		Newsdao newsdao = new Newsdao();
		String ckekId = request.getParameter("ckekId");
		String [] string = ckekId.split(",");
		int [] id = new int[string.length];
		if(string.length > 0) {
			for(int i = 0; i <string.length;i++) {
				id[i] = Integer.parseInt(string[i]);
			}
		}
		int result=-1;
		try {
			for(int i = 0;i<id.length;i++) {
				result =newsdao.doDeleteNewsByID(id[i]);
			}
			if(result >= 0){
				//跳到操作成功画面
				response.getWriter().print("<script type=\"text/javascript\">alert(\"删除成功！\");window.location=\"/Lesson_310/news/news_management.jsp\";</script>");
				return;
			}else {
				response.sendRedirect("/Lesson_310/user/news_success.jsp?count=4");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
